R Bootcamp HTML Slides
Jared Knowles
In this lesson we hope to learn: - How to draw diagnostic plots in base graphics - Colors - `ggplot2’ - Basic geoms - Layering and faceting plots - Putting it together
hist(df$readSS)
plot(df$readSS, df$mathSS)
ggplot2 is pretty much the new standard in Rlibrary(ggplot2)
qplot(readSS, mathSS, data = df)
qplot(readSS, mathSS, data = df) + geom_smooth()
qplot(readSS, mathSS, data = df, alpha = I(0.3))
qplot(readSS, mathSS, data = df) + xlab("Reading Score") + ylab("Math Score")
qplot(readSS, mathSS, data = df, color = race) + scale_color_brewer(type = "qual",
palette = 2)
names(object) helpscolwheel <- "https://dl.dropbox.com/u/1811289/colorwheel.R"
dropbox_source(colwheel)
col.wheel("magenta", nearby = 2)
## [1] "plum" "violet" "darkmagenta" "magenta4" "magenta3"
## [6] "magenta2" "magenta" "magenta1" "orchid4" "orchid"
col.wheel("orange", nearby = 2)
## [1] "salmon1" "darksalmon" "orangered4" "orangered3"
## [5] "coral" "orangered2" "orangered" "orangered1"
## [9] "lightsalmon2" "lightsalmon" "peru" "tan3"
## [13] "darkorange2" "darkorange4" "darkorange3" "darkorange1"
## [17] "linen" "bisque3" "bisque1" "bisque2"
## [21] "darkorange" "antiquewhite3" "antiquewhite1" "papayawhip"
## [25] "moccasin" "orange2" "orange" "orange1"
## [29] "orange4" "wheat4" "orange3" "wheat"
## [33] "oldlace"
col.wheel("brown", nearby = 2)
## [1] "snow1" "snow2" "rosybrown" "rosybrown1" "rosybrown2"
## [6] "rosybrown3" "rosybrown4" "lightcoral" "indianred" "indianred1"
## [11] "indianred3" "brown" "brown4" "brown1" "brown3"
## [16] "brown2" "firebrick" "firebrick1" "chocolate" "chocolate4"
## [21] "saddlebrown" "seashell3" "seashell2" "seashell4" "sandybrown"
## [26] "peachpuff2" "peachpuff3"
library(grid)
p1 <- qplot(readSS, ..density.., data = df, fill = race, position = "fill",
geom = "density") + scale_fill_brewer(type = "qual", palette = 2)
p2 <- qplot(readSS, ..fill.., data = df, fill = race, position = "fill", geom = "density") +
scale_fill_brewer(type = "qual", palette = 2) + ylim(c(0, 1)) + theme_bw() +
opts(legend.position = "none", axis.text.x = theme_blank(), axis.text.y = theme_blank(),
axis.ticks = theme_blank(), panel.margin = unit(0, "lines")) + ylab("") +
xlab("")
vp <- viewport(x = unit(0.65, "npc"), y = unit(0.73, "npc"), width = unit(0.2,
"npc"), height = unit(0.2, "npc"))
print(p1)
print(p2, vp = vp)
ggplot2 can be understood as combining a few conceptsggplot2 has an extended syntax that makes this obviousggplot(df, aes(x = readSS, y = mathSS)) + geom_point()
ggplot2 like a sublanguage under Raes says we are specifying aesthetics, here we specified x and y to make a two dimensional graphic1. 2.
It is good to include the session info, e.g. this document is produced with knitr version 0.7. Here is my session info:
print(sessionInfo(), locale = FALSE)
## R version 2.15.1 (2012-06-22)
## Platform: i386-pc-mingw32/i386 (32-bit)
##
## attached base packages:
## [1] grid stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] stringr_0.6.1 quantreg_4.81 SparseM_0.96 lmtest_0.9-30
## [5] zoo_1.7-7 gridExtra_0.9 ggplot2_0.9.1 hexbin_1.26.0
## [9] lattice_0.20-6 mgcv_1.7-19 Cairo_1.5-1 knitr_0.7
## [13] plyr_1.7.1
##
## loaded via a namespace (and not attached):
## [1] colorspace_1.1-1 dichromat_1.2-4 digest_0.5.2
## [4] evaluate_0.4.2 formatR_0.6 labeling_0.1
## [7] MASS_7.3-19 Matrix_1.0-6 memoise_0.1
## [10] munsell_0.3 nlme_3.1-104 proto_0.3-9.2
## [13] RColorBrewer_1.0-5 reshape2_1.2.1 scales_0.2.1
## [16] tools_2.15.1
This work (R Tutorial for Education, by Jared E. Knowles), in service of the Wisconsin Department of Public Instruction, is free of known copyright restrictions.